import Bridge from './bridge';

export function getAccess() {
  return new Promise((res, rej) => {
    uni.authorize({
      scope: "scope.userLocation",
      success: () => {
        res()
      },
      fail: (err) => {
        err = err["errMsg"];
        uni
          .showModal({
            content: "需要授权位置信息",
            confirmText: "确认授权",
          })
          .then((res) => {
            if (res[1]["confirm"]) {
              uni.openSetting({
                success: (res) => {
                  if (res.authSetting["scope.userLocation"]) {
                    // 授权成功
                    uni.showToast({
                      title: "授权成功",
                      icon: "none",
                    });
                  } else {
                    // 未授权
                    uni.showToast({
                      title: "授权失败",
                      icon: "none",
                    });
                  }
                  res()
                },
              });
            }
            if (res[1]["cancel"]) {
              // 取消授权
              console.log("取消");
              res()
            }
          });
      },
    });
  })
}
export function call(phone) {
  Bridge.callHandler('triggerToCall', JSON.stringify({
    phone
  }), (args) => {
  })
}
export function getLocation() {
  return new Promise((res, rej) => {
    //#ifdef H5
    if (window.location.port) {
      res({
        latitude: 30.250979,
        longitude: 120.163199,
      })
    }
    Bridge.callHandler('triggerToGetLocation', JSON.stringify({}), (args) => {
      try {
        const data = JSON.parse(args);
        const { lat, lon } = data
        res({
          latitude: lat,
          longitude: lon,
        })
      } catch (err) {
        uni.showToast({
          title: '位置获取失败',
          icon: 'none'
        });
      }
    });
    // #endif

    // #ifdef MP-WEIXIN
    uni.getLocation({
      type: "gcj02", // 默认gps 坐标
      geocode: true,
      altitude: true, // 是否返回高度
      accuracy: "best", // 精度值为20m
      success(res) {
        res(res);
      },
      fail(err) {
        if (
          err.errMsg ===
          "getLocation:fail 频繁调用会增加电量损耗，可考虑使用 wx.onLocationChange 监听地理位置变化"
        ) {
          uni.showToast({
            title: "请勿频繁定位",
            icon: "none",
          });
        }
        if (err.errMsg === "getLocation:fail auth deny") {
          // 未授权
          uni.showToast({
            title: "无法定位，请重新获取位置信息",
            icon: "none",
          });
          rej();
        }
        if (
          err.errMsg ===
          "getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF"
        ) {
          uni.showModal({
            content: "请开启手机定位服务",
            showCancel: false,
          });
        }
      },
    });
    // #endif
  })
}


export function getSetting() {
  return new Promise((res, rej) => {
    //#ifdef H5
    res();
    // #endif

    // #ifdef MP-WEIXIN

    uni.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          uni.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                uni.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                uni.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      uni.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用uni.getLocation的API
                      res()
                    } else {
                      uni.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用uni.getLocation的API
          res()
        } else {
          //调用uni.getLocation的API
          res()
        }
      }
    })
    // #endif

  })
}